package com.kamloo.stg.fragment;

import java.util.ArrayList;

import com.kamloo.stg.adapter.TabBookmarkArrayAdapter;
import com.kamloo.stg.data.db.Bookmark;
import com.kamloo.stg.data.db.BookmarkDAO;
import com.kamloo.stg.data.db.Direction;
import com.kamloo.stg.data.db.DirectionDAO;
import com.kamloo.stg.data.db.Line;
import com.kamloo.stg.data.db.LineDAO;
import com.kamloo.stg.data.db.Node;
import com.kamloo.stg.data.db.NodeDAO;
import com.kamloo.stg.data.db.Stop;
import com.kamloo.stg.data.db.StopDAO;
import com.kamloo.stg.dialog.TabBookmarkPeriodDialog;
import com.kamloo.stg.info.AppConstant;
import com.kamloo.stg.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class TabBookmarks extends Fragment {

	private ListView m_bookmarkList;
	ArrayList<String> m_values;
	ArrayList<Stop> m_stops;
	ArrayList<Node> m_nodes;
	ArrayList<String> m_lines;
	ArrayList<String> m_directions;
	
	//----- LISTENER -----
	OnItemClickListener bookmarkItemClickListener = new OnItemClickListener()
    {
    	public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
    	{
    		FragmentManager manager = getActivity().getSupportFragmentManager();
    		TabBookmarkPeriodDialog dialog = new TabBookmarkPeriodDialog();
   		    
    		int nodeId = Integer.parseInt((String) ((TextView)view.findViewById(R.id.tabBookmarkRowNodeId)).getText());
    		    		    		    		
    		dialog.initializeView(nodeId);    		
    		dialog.show(manager, AppConstant.FRAGMENT_TAB_BOOKMARKS_PERIOD_DIALOG);
    	}
    };	    

	// ----- CLASS CODE -----
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		if (container == null) {
			return null;
		}

		return (LinearLayout) inflater.inflate(R.layout.tab_bookmarks, container, false);
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		m_bookmarkList = (ListView) view.findViewById(R.id.tabBookmarkList);
		initializeData();
	}	
		
	private void initializeData(){
		m_values = new ArrayList<String>();
		m_stops = new ArrayList<Stop>();
		m_nodes = new ArrayList<Node>();
		m_lines = new ArrayList<String>();
		m_directions = new ArrayList<String>();
		
		setBookmarkData();
		TabBookmarkArrayAdapter adapter = new TabBookmarkArrayAdapter(getActivity(), m_values.toArray(new String[m_values.size()]), 
				                                                m_stops, m_nodes, m_lines, m_directions);

		// Assign adapter and listener to ListView
		m_bookmarkList.setAdapter(adapter);
		m_bookmarkList.setOnItemClickListener(bookmarkItemClickListener);
	}

	private void setBookmarkData() {
		BookmarkDAO bookmarkDao = new BookmarkDAO(getActivity());
		NodeDAO nodeDao = new NodeDAO(getActivity());
		StopDAO stopDao = new StopDAO(getActivity());
		LineDAO lineDao = new LineDAO(getActivity());
		DirectionDAO directionDao = new DirectionDAO(getActivity());
    	nodeDao.open();
    	stopDao.open();
    	bookmarkDao.open();
    	directionDao.open();
    	lineDao.open();
    	
    	ArrayList<Bookmark> bookmarks = bookmarkDao.selectAll();
    	for (Bookmark bookmark : bookmarks) {    		
    		Node node = nodeDao.select(bookmark.getNodeId());    		    		
    		Stop stop = stopDao.select(node.getFk_stopId());
    		    		
    		Direction direction = directionDao.select(node.getFk_directionId());
    		Line line = lineDao.select(direction.getFk_lineId());
    		
    		String name = line.getIdentifier();
    		if (name.contains("STS"))
    	    {
    			name = name.replace("STS", "");
    			name = name.substring(0, name.indexOf("_"));
    	    }
    		
    		m_directions.add(direction.getIdentifier());
    		m_lines.add(name);
    		m_nodes.add(node);
    		m_stops.add(stop);
    		m_values.add(stop.getName());
    	}
    	
    	directionDao.close();
		lineDao.close();
    	nodeDao.close();
    	stopDao.close();
    	bookmarkDao.close();
	}
}
